package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.Mdzx;
import cn.stylefeng.guns.sys.modular.system.model.params.MdzxParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxTzqkParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxTzqkViewResult;
import cn.stylefeng.guns.sys.modular.system.service.MdzxService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * 门店装修表控制器
 *
 * @author jiangge
 * @Date 2020-11-02 09:09:43
 */
@Controller
@RequestMapping("/dgdmdzx")
public class DgdMdzxController extends BaseController {

    private Long pinpai = 1417361607353536514L;

    private String PREFIX = "/modular/system/dgdmdzx";

    @Autowired
    private MdzxService mdzxService;

    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/mdzx.html";
    }

    @RequestMapping("index2")
    public String index2() {
        return PREFIX + "/mdzx2.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/mdzx_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/mdzx_edit.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * param  btyZxMdzxId 被复制的门店id
     * @Date 2020-11-02
     */
    @RequestMapping("/copy")
    public String copy(Model model, Long btyZxMdzxId) {
        model.addAttribute("copiedBtyZxMdzxId", btyZxMdzxId);
        return PREFIX + "/mdzx_copy.html";
    }

    /**
     * 投资情况-点击金额时弹出计算器页面
     *
     * @author jiangge
     * param  btyZxMdzxId 被复制的门店id
     * @Date 2020-11-02
     */
    @RequestMapping("/jinejisuan")
    public String jinejisuan(Model model, ZxTzqkParam mdzxParam) {
        if (mdzxParam.getJine() == null) {
            mdzxParam.setJine(BigDecimal.ZERO.setScale(0));
        }
        model.addAttribute("param", mdzxParam);
        return PREFIX + "/mdzx_jinejisuan.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * param  btyZxMdzxId 被复制的门店id
     * @Date 2020-11-02
     */
    @RequestMapping("/tzqkMingXi")
    public String tzqkMingXi() {
        return PREFIX + "/mdzx_tzqkMingXi.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(MdzxParam mdzxParam) {
        mdzxParam.setPinpai(this.pinpai);
        ResponseData responseData = this.mdzxService.add(mdzxParam);
        return responseData;
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MdzxParam mdzxParam) {
        mdzxParam.setPinpai(this.pinpai);
        ResponseData data = this.mdzxService.update(mdzxParam);
        return data;
    }

    /**
     * 投资预算-复制
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/copyItem")
    @ResponseBody
    public ResponseData copyItem(MdzxParam mdzxParam) {
        mdzxParam.setPinpai(this.pinpai);

        ResponseData data = this.mdzxService.copyZxYuSuan(mdzxParam);
        return data;
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(MdzxParam mdzxParam) {
        ResponseData data = this.mdzxService.delete(mdzxParam);
        return data;
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(MdzxParam mdzxParam) {
        mdzxParam.setPinpai(this.pinpai);

        Mdzx detail = this.mdzxService.getById(mdzxParam.getBtyZxMdzxId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(MdzxParam mdzxParam) {
        mdzxParam.setPinpai(this.pinpai);
        return this.mdzxService.findPageBySpec(mdzxParam);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/mdzxList")
    public ResponseData mdzxList(MdzxParam mdzxParam) {
        mdzxParam.setPinpai(this.pinpai);
        List<Map<String, Object>> maps = this.mdzxService.mdzxList(mdzxParam);
        return ResponseData.success(maps);
    }

    @ResponseBody
    @RequestMapping("/ghsList")
    public ResponseData ghsList(MdzxParam mdzxParam) {
        mdzxParam.setPinpai(this.pinpai);
        List<Map<String, Object>> maps = this.mdzxService.ghsList(mdzxParam);
        return ResponseData.success(maps);
    }

    // 投资情况表导出excel
    @RequestMapping("/listExport")
    public ResponseEntity<byte[]> exportExcel(HttpServletRequest request, HttpServletResponse response, Long btyZxMdzxId) {
        return this.mdzxService.exportExcel(request, response, btyZxMdzxId);
    }

    // 投资情况表导出excel-带单位预估值和百分比的导出
    @RequestMapping("/listExport2")
    public ResponseEntity<byte[]> exportExcel2(HttpServletRequest request, HttpServletResponse response, Long btyZxMdzxId) {
        return this.mdzxService.exportExcel2(request, response, btyZxMdzxId);
    }


    // 投资情况表导出excel-带公式
    @RequestMapping("/listExportWithGongshi")
    public ResponseEntity<byte[]> exportExcelWithGongshi(HttpServletRequest request, HttpServletResponse response, Long btyZxMdzxId) {
        return this.mdzxService.exportExcelWithGongshi(request, response, btyZxMdzxId);
    }


    /**
     * 跳转到投资情况展示页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/investmentSituation")
    public String index1(Model model, MdzxParam param) {
        param.setPinpai(this.pinpai);
        Mdzx mdzx = this.mdzxService.getById(param.getBtyZxMdzxId());
        model.addAttribute("mdInfo", mdzx);
        return PREFIX + "/investmentSituation.html";
    }

    /**
     * 跳转到投资情况展示页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/tzqk")
    public String tzqk(Model model, MdzxParam param) {
        Mdzx mdzx = this.mdzxService.getById(param.getBtyZxMdzxId());
        model.addAttribute("mdInfo", mdzx);
        return PREFIX + "/tzqk.html";
    }

    /**
     * 跳转到投资情况展示页面(百分比)
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/tzqk2")
    public String tzqk2(Model model, MdzxParam param) {
        param.setPinpai(this.pinpai);
        Mdzx mdzx = this.mdzxService.getById(param.getBtyZxMdzxId());
        //单位面积预估值
        BigDecimal unitYusuanHeji = BigDecimal.ZERO;
        mdzx.setUnitYusuanHeji(unitYusuanHeji);
        if(mdzx.getYujiTouzi().compareTo(BigDecimal.ZERO)!=0&&!StringUtils.isEmpty(mdzx.getMianji())){
            mdzx.setUnitYusuanHeji(mdzx.getYujiTouzi().divide(new BigDecimal(mdzx.getMianji()),2,BigDecimal.ROUND_UP));
        }

        model.addAttribute("mdInfo", mdzx);
        return PREFIX + "/tzqk2.html";
    }

    /**
     * 投资情况具体信息：所有成本明细及相关科目信息展示
     *
     * @param param
     * @return
     */

    @RequestMapping("/investmentSituationInfo")
    @ResponseBody
    public LayuiPageInfo lisst(MdzxParam param) {
        param.setPinpai(this.pinpai);
        //查询列表数据
        List<ZxTzqkViewResult> tzqkInfoList = this.mdzxService.getInvestmentSituation(param.getBtyZxMdzxId());
        int total = tzqkInfoList.size();
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(tzqkInfoList);
        layuiPageInfo.setCount(total);
        return layuiPageInfo;
    }

    /**
     * 投资情况具体信息：所有成本明细及相关科目信息展示
     *
     * @param param
     * @return
     */

    @RequestMapping("/tzqkInfo")
    @ResponseBody
    public LayuiPageInfo tzqkInfo(MdzxParam param) {
        param.setPinpai(this.pinpai);
        //查询列表数据
        List<ZxTzqkViewResult> tzqkInfoList = this.mdzxService.getTzqkDgd(param.getBtyZxMdzxId());
        int total = tzqkInfoList.size();
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(tzqkInfoList);
        layuiPageInfo.setCount(total);
        return layuiPageInfo;
    }

    /**
     * 门店复制
     *
     * @param param
     * @return
     */

    @RequestMapping("/copyzx")
    @ResponseBody
    public ResponseData copyzx(MdzxParam param) {
        if (ToolUtil.isEmpty(param.getBtyZxMdzxId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        param.setPinpai(this.pinpai);
        ResponseData responseData = this.mdzxService.copyZx(param);
        return responseData;
    }

    /**
     * 门店复制
     *
     * @param param
     * @return
     */

    @RequestMapping("/copyYusn")
    @ResponseBody
    public ResponseData copyYusn(MdzxParam param) {
        if (ToolUtil.isEmpty(param.getBtyBrandId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        param.setPinpai(this.pinpai);
        return this.mdzxService.copyYusn(param);
    }


    /**
     * 修改预计投资
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editYujiTouzi")
    @ResponseBody
    public ResponseData editYujiTouzi(MdzxParam param) {
        param.setPinpai(this.pinpai);
        return this.mdzxService.editYujiTouzi(param);
    }

    /**
     * 门店锁定
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/yuSuanLockEdit")
    @ResponseBody
    public ResponseData yuSuanLockEdit(MdzxParam param) {
        param.setPinpai(this.pinpai);
        return this.mdzxService.yusuanLockEdit(param);
    }

    @RequestMapping("/lock")
    @ResponseBody
    public ResponseData lock(String records) {
        List<MdzxParam> params = JSONArray.parseArray(records, MdzxParam.class);
        return this.mdzxService.lock(params);
    }

    @RequestMapping("/export")
    public ResponseEntity<byte[]> export(HttpServletRequest request, HttpServletResponse response, MdzxParam param){
        return this.mdzxService.exportMx(request,response,param);
    }

}


